#include <iostream>
#include <set>

//被观察者基类
class observeBase
{
public:
    virtual std::string GetName() = 0;  //获取观察者名称
    virtual void Update(const std::string & news) = 0;//更新订阅消息
};

//观察者具体类
class observer:public observeBase
{
public:
    observer(const std::string& name):name(name){}
    std::string GetName()
    {
        return name;
    }
    void Update(const std::string & news)
    {
        this->news = news;
        std::cout<<"我是"<<name<<","<<"我收到了消息"<<news<<std::endl;
    }
private:
    std::string name;
    std::string news;

};

//被观察者基类
class TargeBase
{
public:
  virtual void AddObserver(observeBase * ob) = 0;  //添加观察者
  virtual void RMObserver(observeBase * ob) = 0;   //删除观察者
  virtual void BroadCast(const std::string & news) = 0; //更新内容
};

class Target:public TargeBase
{
public:
    Target(const std::string& name):name(name){}
    void AddObserver(observeBase *ob)
    {
        s.insert(ob);
        std::cout<<ob->GetName()<<"绑定了"<<name<<"的消息"<<std::endl;
    }
    void RMObserver(observeBase * ob)
    {
        auto ite = s.find(ob);
        if(ite == s.end())
        {
            std::cout<<"该用户不是你的观察者"<<std::endl;
        }
        else
        {
            s.erase(ite);
            std::cout<<"该用户不再是你的观察者"<<std::endl;
        }

    }
    void BroadCast(const std::string & news)
    {
        for(auto & ob : s)
        {
            ob->Update(news);
        }
    }
private:
    std::string name;
    std::set<observeBase*> s; //观察者容器
};

int main()
{
    TargeBase *t = new Target("主播zhangsan");
   
    observeBase *b1 = new observer("李四");
    observeBase *b2 = new observer("王五");

    t->AddObserver(b1);
    t->AddObserver(b2);

    t->BroadCast("你们好");

    t->RMObserver(b2);
    t->BroadCast("sd");
    return 0;

}